package cn.apestar.common.inteceptor;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import cn.apestar.common.DataContants;
import cn.apestar.common.exception.BaseException;


public class DefaultInteceptor implements HandlerInterceptor {

	
	public void afterCompletion(HttpServletRequest arg0,HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		
	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
	}

	/**
	 * 该方法会在Controller的方法执行前会被调用，可以使用这个方法来中断或者继续执行链的处理，当返回true时，处理执行链会继续，当返回false时，则不会去执行Controller的方法。（验证用户是否登陆就是使用preHandleAction方法的最好例子）
	 */
	public boolean preHandle(HttpServletRequest req, HttpServletResponse arg1,Object arg2) throws Exception {
		// TODO Auto-generated method stub
		StringBuffer url = req.getRequestURL();
		return true;
	}
	
	/**
	 * 把表单的数据根据文档 和 数据分开组装
	 * @param map
	 * @param name
	 * @param item
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public void createMap(Map map,FileItem item) throws UnsupportedEncodingException{
	      String name = item.getFieldName();   
	       
          if(item.isFormField()){
        	  map.put(name,item.getString("UTF-8"));
          }else{
        	  map.put(name,item);
          }
	}
	
	/**
	 * 处理HEADER
	 */
	public void getHeader(HttpServletRequest req){
		String version = req.getHeader("version");
		String ticke   = req.getHeader("ticke");
		//处理tick 为当前用户
	}
}
